/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.util;

import pikes.core.Printable;

/**
 * Utility class similar to Spring's but dealing rather with JDK 5's CharSequence than String's.
 * @author Peter Bona
 * @see org.springframework.util.Assert
 */
public final class Assert {

	private Assert() {
		// no op
	}

	/**
	 * Checks if the string provided as CharSequence has a valid text, meaning it cannot be null, cannot be empty and
	 * has to contain at least one non-whitespace character. If the condition is not met, it throws an
	 * IllegalArgumentException with the message 'Parameter string has no text'.
	 * @see Assert#hasText(CharSequence, String)
	 * @see org.springframework.util.Assert#hasText(String)
	 * @param string to check
	 */
	public static void hasText(final CharSequence string) {
		hasText(string, "Parameter string has no text.");
	}

	/**
	 * Checks if the string provided as CharSequence has a valid text, meanining it cannot be null, cannot be empty and
	 * has to contain at least one non-whitespace character. If the condition is not met, it throws an
	 * IllegalArgumentException with the specified message.
	 * @see org.springframework.util.Assert#hasText(String,String)
	 * @param string to check
	 * @param message Error message
	 */
	public static void hasText(final CharSequence string, final String message) {
		if (!Util.hasText(string)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Checks if the two character sequences are the same. If not, IllegalArgumentException is thrown.
	 * @param string1
	 * @param string2
	 */
	public static void equals(final CharSequence string1, final CharSequence string2) {
		if (!Util.equals(string1, string2)) {
			throw new IllegalArgumentException("The strings do not equal.");
		}
	}

	/**
	 * Checks if the two character sequences are the same. If not, IllegalArgumentException is thrown.
	 * @param string1
	 * @param string2
	 */
	public static void equals(final Printable string1, final Printable string2) {
		if (!Util.equals(string1, string2)) {
			throw new IllegalArgumentException("The strings do not equal.");
		}
	}

	/**
	 * Checks if the object is null. If so, IllegalArgumentException is thrown.
	 * @param object to be checked
	 */
	public static void notNull(final Object object) {
		org.springframework.util.Assert.notNull(object);
	}
}
